package motor;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import module.ModuleStateImp;
import module.ModuleStateImpU;

import utils.Vector2d;

public abstract class Unite
{
	ModuleArme arme;
	ModuleMobile mobile;
	ModuleState mstate;
	ModuleLife mlife;
	
	int camp;
	Color color;

	protected ArrayList<Unite> ennemis;
	
	protected int	size = 4;
	private Vector2d pos = new Vector2d(-1,-1);
	private float vision = 150;
	
	public Unite( float x , float y , int camp )
	{
		pos.x = x;
		pos.y = y;
		this.camp = camp;
		this.arme = null;
		this.mobile = null;
	}

	public void draw(Graphics buffer_canvas)
	{
		buffer_canvas.setColor( color );
		
		if( mobile == null )
			buffer_canvas.fillRect((int)(pos.x-size/2), (int)(pos.y-size/2), size , size);
		else
			buffer_canvas.fillOval((int)(pos.x-size/2), (int)(pos.y-size/2), size , size);
		
		
		buffer_canvas.setColor( Color.blue );
		buffer_canvas.drawOval((int)(pos.x-vision), (int)(pos.y-vision), (int)vision*2 , (int)vision*2);
		
		
		mlife.draw( buffer_canvas , pos , size );
		
		if( arme != null )
			arme.draw( buffer_canvas );
	}

	public Vector2d getPos()
	{
		return pos;
	}

	public ModuleState getMstate()
	{
		return mstate;
	}

	public void setMstate(ModuleState mstate)
	{
		this.mstate = mstate;
	}

	public void move(Motor motor)
	{
		if( mobile != null )
			mobile.move(motor);
		else
			System.err.println("Ne peut pas bouger!!!");
	}

	public void setVtemp(Vector2d vector2d)
	{
		if( mobile != null )
			mobile.setVtemp(vector2d);
		else
			System.err.println("Ne peut pas bouger!!!");
	}

	public ModuleMobile getMobile()
	{
		return mobile;
	}
	
	public float distance(Vector2d point)
	{
		return (float) Math.sqrt(Math.pow(pos.x-point.x, 2 ) + Math.pow(pos.y-point.y, 2 ));
	}

	public void goOn(Motor motor)
	{
		if( mstate != null )
			mstate.goOn( motor );
		else
			System.err.println("Ne peut pas penser!!!");
		
	}

	public void tir(Motor motor)
	{
		if( arme != null && mstate != null )
			arme.feu(motor, mstate.getCible() );
		else
			System.err.println("Ne peut pas tirer!!!");
	}

	public void damage(float degats)
	{
		mlife.damage( degats );
	}

	public boolean isDead()
	{
		return mlife.isDead();
	}

	public float getVision()
	{
		return vision;
	}

	public float getPortee()
	{
		return arme.getPortee();
	}

	public int getCamp()
	{
		return camp;
	}

	public ArrayList<Unite> getEnnemis()
	{
		return ennemis;
	}

	public void setEnnemis(ArrayList<Unite> ennemis)
	{
		this.ennemis = ennemis;
	}


	public float getInfluence()
	{
		return 1000;
	}
	
	public void setMobile(ModuleMobile mobile)
	{
		this.mobile = mobile;
	}

	public int getSize()
	{
		return size;
	}
}
